package com.lanou3g.ptp.api.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lanou3g.ptp.api.service.ApiGradeService;
import com.lanou3g.ptp.entity.*;
import com.lanou3g.ptp.grade.service.GradeService;
import com.lanou3g.ptp.grade.service.GradeRoleService;
import com.lanou3g.ptp.taskrole.service.TaskRoleService;
import com.lanou3g.ptp.user.service.UserService;
import com.lanou3g.utils.PtpResult;
import com.lanou3g.utils.PtpResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Api(description = "成绩接口")
@RestController
public class GradeController {
    @Reference
    private GradeService gradeService;
    @Reference
    private GradeRoleService gradeRoleService;
    @Reference
    private TaskRoleService taskRoleService;
    @Reference
    private UserService userService;
    @Autowired
    private ApiGradeService apiGradeService;

//  添加一个学生的信息
    @ApiOperation("添加一个学生的信息")
    @PostMapping("/grade/init")
    public PtpResult addGrade(Grade grade){
        try{
            gradeService.insertGrade(grade);
            return PtpResultUtils.ok("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return PtpResultUtils.error(533);
        }
    }
//    删除一个学生的信息
    @ApiOperation("删除一个学生的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "gradeRoleId")
    })
    @DeleteMapping("/grade/init/{id}")
    public PtpResult deleteGradeById(@PathVariable Long id){
        try{
            gradeRoleService.deleteByGradeId(id);
            gradeService.deleteById(id);
            return PtpResultUtils.ok("删除成功");
        }catch(Exception e){
            e.printStackTrace();
            return PtpResultUtils.error(533);
        }

    }
//    查找所有学生成绩
    @ApiOperation("查找所有学生的信息")
    @GetMapping("/grade")
    public PtpResult findAll() {
        try{
            InputStream all = apiGradeService.findAll();
            return PtpResultUtils.ok(all);
        }catch (Exception e){
            e.printStackTrace();
            return PtpResultUtils.error(533);
        }

    }

}
